package com.planet.merchant.controller;

import com.planet.common.core.domain.model.LoginUser;
import com.planet.merchant.service.IFileService;
import com.planet.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.planet.common.annotation.Log;
import com.planet.common.core.controller.BaseController;
import com.planet.common.core.domain.AjaxResult;
import com.planet.common.enums.BusinessType;
import com.planet.merchant.domain.PmsProductCodeBind;
import com.planet.merchant.service.IPmsProductCodeBindService;
import com.planet.common.utils.poi.ExcelUtil;
import com.planet.common.core.page.TableDataInfo;


import java.util.List;

/**
 * 二维码绑定关系Controller
 *
 * @author ggm
 * @date 2025-03-29
 */
@RestController
@RequestMapping("/merchant/bind")
public class PmsProductCodeBindController extends BaseController {
    @Autowired
    private IPmsProductCodeBindService pmsProductCodeBindService;
    @Autowired
    private IFileService fileService;


    /**
     * 查询二维码绑定关系列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:bind:list')")
    @GetMapping("/list")
    public TableDataInfo list(PmsProductCodeBind pmsProductCodeBind) {
        startPage();
        List<PmsProductCodeBind> list = pmsProductCodeBindService.selectPmsProductCodeBindList(pmsProductCodeBind);
        return getDataTable(list);
    }

    /**
     * 导出二维码绑定关系列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:bind:export')")
    @Log(title = "二维码绑定关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsProductCodeBind pmsProductCodeBind) {
        List<PmsProductCodeBind> list = pmsProductCodeBindService.selectPmsProductCodeBindList(pmsProductCodeBind);
        ExcelUtil<PmsProductCodeBind> util = new ExcelUtil<PmsProductCodeBind>(PmsProductCodeBind.class);
        util.exportExcel(response, list, "二维码绑定关系数据");
    }

    /**
     * 获取二维码绑定关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:bind:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pmsProductCodeBindService.selectPmsProductCodeBindById(id));
    }

    /**
     * 新增二维码绑定关系
     */
    @PreAuthorize("@ss.hasPermi('merchant:bind:add')")
    @Log(title = "二维码绑定关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsProductCodeBind pmsProductCodeBind) {
        return toAjax(pmsProductCodeBindService.insertPmsProductCodeBind(pmsProductCodeBind));
    }

    /**
     * 修改二维码绑定关系
     */
    @PreAuthorize("@ss.hasPermi('merchant:bind:edit')")
    @Log(title = "二维码绑定关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsProductCodeBind pmsProductCodeBind) {
        return toAjax(pmsProductCodeBindService.updatePmsProductCodeBind(pmsProductCodeBind));
    }

    /**
     * 删除二维码绑定关系
     */
    @PreAuthorize("@ss.hasPermi('merchant:bind:remove')")
    @Log(title = "二维码绑定关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsProductCodeBindService.deletePmsProductCodeBindByIds(ids));
    }

    /**
     * @Author: ggm
     * @date: 2025/3/30
     * 查询树状二维码绑定关系
     */
    @PreAuthorize("@ss.hasPermi('merchant:bind:query')")
    @GetMapping("/tree")
    public AjaxResult getTreeInfo() {
        return success(pmsProductCodeBindService.selectPmsProductCodeBindTree());
    }

    /**
     * @Author: ggm
     * @date: 2025/3/31
     * 文件上传
     */
    //@PreAuthorize("@ss.hasPermi('merchant:bind:add')")
    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        String fileUrl = fileService.uploadFile(file);
        return AjaxResult.success("文件上传成功: " + fileUrl);
    }

    /**
     * @Author: ggm
     * @date: 2025/4/1
     * 根据level3code删除二维码绑定关系
     */
    @PreAuthorize("@ss.hasPermi('merchant:bind:remove')")
    @Log(title = "二维码绑定关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/byLevel3Code/{level3code}")
    public AjaxResult removeByLevel3Code(@PathVariable String level3code) {
        return toAjax(pmsProductCodeBindService.deletePmsProductCodeBindByLeve3Code(level3code));
    }

    @PreAuthorize("@ss.hasPermi('merchant:bind:edit')")
    @Log(title = "二维码绑定关系", businessType = BusinessType.UPDATE)
    @GetMapping("updateBindByLevel3code")
    public AjaxResult editByLeve3Code(@RequestParam("level3code") String level3code,
                                      @RequestParam("newLevel3code") String newLevel3code) {
        return toAjax(pmsProductCodeBindService.updateBindByLevel3code(level3code, newLevel3code));
    }

    /**
     * @Author: ggm
     * @date: 2025/4/6
     * 根据id入库
     */
    @PutMapping(value = "/store/{id}")
    public AjaxResult store(@PathVariable("id") Long id) {
        return success(pmsProductCodeBindService.storeById(id));
    }
     /**
      * @Author: ggm
      * @date:  2025/4/6
      * 根据level3Code入库
      */
     @PutMapping(value = "/storeByLevel3Code/{level3code}")
     public AjaxResult storeByLevel3Code(@PathVariable("level3code") String level3code) {
         return success(pmsProductCodeBindService.storeByLevel3Code(level3code));
     }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PmsProductCodeBind> util = new ExcelUtil<PmsProductCodeBind>(PmsProductCodeBind.class);
        List<PmsProductCodeBind> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUsername();
        String message = pmsProductCodeBindService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<PmsProductCodeBind> util = new ExcelUtil<PmsProductCodeBind>(PmsProductCodeBind.class);
        return util.importTemplateExcel("用户数据");
    }


}
